home *** CD-ROM | disk | FTP | other *** search
-
- /* cylinder drawing demo */
-
- /* required include files */
- #include <GL/gl.h>
- #include "glut.h"
- #include "GL/tube.h"
-
- /* the arrays in which we will store out polyline */
- #define NPTS 6
- double points [NPTS][3];
- float colors [NPTS][3];
- int idx = 0;
-
- /* some utilities for filling that array */
- #define PNT(x,y,z) { \
- points[idx][0] = x; \
- points[idx][1] = y; \
- points[idx][2] = z; \
- idx ++; \
- }
-
- #define COL(r,g,b) { \
- colors[idx][0] = r; \
- colors[idx][1] = g; \
- colors[idx][2] = b; \
- }
-
- /*
- * Initialize a bent shape with three segments.
- * The data format is a polyline.
- *
- * NOTE that neither the first, nor the last segment are drawn.
- * The first & last segment serve only to determine that angle
- * at which the endcaps are drawn.
- */
-
- void InitCyl (void) {
-
- COL (0.0, 0.0, 0.0);
- PNT (-6.0, 6.0, 0.0);
-
- COL (0.0, 0.8, 0.3);
- PNT (6.0, 6.0, 0.0);
-
- COL (0.8, 0.3, 0.0);
- PNT (6.0, -6.0, 0.0);
-
- COL (0.2, 0.3, 0.9);
- PNT (-6.0, -6.0, 0.0);
-
- COL (0.2, 0.8, 0.5);
- PNT (-6.0, 6.0, 0.0);
-
- COL (0.0, 0.0, 0.0);
- PNT (6.0, 6.0, 0.0);
- }
-
- float lastx=0;
- float lasty=0;
-
- /* draw the cylinder shape */
- void DrawCyl (void) {
-
- glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- /* set up some matrices so that the object spins with the mouse */
- glPushMatrix ();
- glTranslatef (0.0, 0.0, -80.0);
- glRotatef (lastx, 0.0, 1.0, 0.0);
- glRotatef (lasty, 1.0, 0.0, 0.0);
-
- /* Phew. FINALLY, Draw the polycylinder -- */
- gleSetJoinStyle (TUBE_NORM_EDGE | TUBE_JN_ANGLE | TUBE_JN_CAP);
- glePolyCylinder (NPTS, points, colors, 2.3);
-
- glPopMatrix ();
-
- glutSwapBuffers ();
- }
-
- /* get notified of mouse motions */
- void MouseMotion (int x, int y)
- {
- lastx = x;
- lasty = y;
- glutPostRedisplay ();
- }
-
-
- /* set up a light */
- GLfloat lightOnePosition[] = {40.0, 40, 100.0, 0.0};
- GLfloat lightOneColor[] = {0.99, 0.99, 0.99, 1.0};
-
- GLfloat lightTwoPosition[] = {-40.0, 40, 100.0, 0.0};
- GLfloat lightTwoColor[] = {0.99, 0.99, 0.99, 1.0};
-
- main (int argc, char * argv[]) {
-
- /* initialize glut */
- glutInit (&argc, argv);
- glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutCreateWindow ("cylinder");
- glutDisplayFunc (DrawCyl);
- glutMotionFunc (MouseMotion);
-
- /* initialize GL */
- glClearDepth (1.0);
- glEnable (GL_DEPTH_TEST);
- glClearColor (0.0, 0.0, 0.0, 0.0);
- glShadeModel (GL_SMOOTH);
-
- glMatrixMode (GL_PROJECTION);
- /* roughly, measured in centimeters */
- glFrustum (-9.0, 9.0, -9.0, 9.0, 50.0, 150.0);
- glMatrixMode(GL_MODELVIEW);
-
- /* initialize lighting */
- glLightfv (GL_LIGHT0, GL_POSITION, lightOnePosition);
- glLightfv (GL_LIGHT0, GL_DIFFUSE, lightOneColor);
- glEnable (GL_LIGHT0);
- glLightfv (GL_LIGHT1, GL_POSITION, lightTwoPosition);
- glLightfv (GL_LIGHT1, GL_DIFFUSE, lightTwoColor);
- glEnable (GL_LIGHT1);
- glEnable (GL_LIGHTING);
- glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
- glEnable (GL_COLOR_MATERIAL);
-
- InitCyl ();
-
- glutMainLoop ();
- }
-